home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / dflat_r_.arc / MEMOPAD.C < prev    next >
Text File  |  1991-10-02  |  11KB  |  350 lines

  1. /* --------------- memopad.c ----------- */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys\types.h>
  7. #include <sys\stat.h>
  8. #include <dos.h>
  9. #ifndef MSC
  10. #ifndef WATCOM
  11. #include <dir.h>
  12. #endif
  13. #endif
  14. #include "dflat.h"
  15.  
  16. char DFlatApplication[] = "MemoPad";
  17.  
  18. static char Untitled[] = "Untitled";
  19. static int wndpos;
  20.  
  21. static int MemoPadProc(WINDOW, MESSAGE, PARAM, PARAM);
  22. static void NewFile(WINDOW);
  23. static void SelectFile(WINDOW);
  24. static void PadWindow(WINDOW, char *);
  25. static void OpenPadWindow(WINDOW, char *);
  26. static void LoadFile(WINDOW, int);
  27. static void PrintPad(WINDOW);
  28. static void SaveFile(WINDOW, int);
  29. static void DeleteFile(WINDOW);
  30. static int EditorProc(WINDOW, MESSAGE, PARAM, PARAM);
  31. static char *NameComponent(char *);
  32. char **Argv;
  33.  
  34. void main(int argc, char *argv[])
  35. {
  36.     WINDOW wnd;
  37.     init_messages();
  38.     Argv = argv;
  39.     wnd = CreateWindow(APPLICATION,
  40.                         "D-Flat MemoPad " VERSION,
  41.                         0, 0, -1, -1,
  42.                         &MainMenu,
  43.                         NULL,
  44.                         MemoPadProc,
  45.                         MOVEABLE  |
  46.                         SIZEABLE  |
  47.                         HASBORDER |
  48.                         HASSTATUSBAR
  49.                         );
  50.  
  51.     SendMessage(wnd, SETFOCUS, TRUE, 0);
  52.     while (argc > 1)    {
  53.         PadWindow(wnd, argv[1]);
  54.         --argc;
  55.         argv++;
  56.     }
  57.     while (dispatch_message())
  58.         ;
  59. }
  60. /* ------ open text files and put them into editboxes ----- */
  61. static void PadWindow(WINDOW wnd, char *FileName)
  62. {
  63.     int ax, criterr = 1;
  64.     struct ffblk ff;
  65.     char path[64];
  66.     char *cp;
  67.  
  68.     CreatePath(path, FileName, FALSE, FALSE);
  69.     cp = path+strlen(path);
  70.     CreatePath(path, FileName, TRUE, FALSE);
  71.     while (criterr == 1)    {
  72.         ax = findfirst(path, &ff, 0);
  73.         criterr = TestCriticalError();
  74.     }
  75.     while (ax == 0 && !criterr)    {
  76.         strcpy(cp, ff.ff_name);
  77.         OpenPadWindow(wnd, path);
  78.         ax = findnext(&ff);
  79.     }
  80. }
  81. /* ------- window processing module for the
  82.                     memopad application window ----- */
  83. static int MemoPadProc(WINDOW wnd,MESSAGE msg,PARAM p1,PARAM p2)
  84. {
  85.     switch (msg)    {
  86.         case COMMAND:
  87.             switch ((int)p1)    {
  88.                 case ID_NEW:
  89.                     NewFile(wnd);
  90.                     return TRUE;
  91.                 case ID_OPEN:
  92.                     SelectFile(wnd);
  93.                     return TRUE;
  94.                 case ID_SAVE:
  95.                     SaveFile(inFocus, FALSE);
  96.                     return TRUE;
  97.                 case ID_SAVEAS:
  98.                     SaveFile(inFocus, TRUE);
  99.                     return TRUE;
  100.                 case ID_DELETEFILE:
  101.                     DeleteFile(inFocus);
  102.                     return TRUE;
  103.                 case ID_PRINT:
  104.                     PrintPad(inFocus);
  105.                     return TRUE;
  106.                 case ID_ABOUT:
  107.                     MessageBox(
  108.                          "About D-Flat and the MemoPad",
  109.                         "   ┌───────────────────────┐\n"
  110.                         "   │    ▄▄▄   ▄▄▄     ▄    │\n"
  111.                         "   │    █  █  █  █    █    │\n"
  112.                         "   │    █  █  █  █    █    │\n"
  113.                         "   │    █  █  █  █ █  █    │\n"
  114.                         "   │    ▀▀▀   ▀▀▀   ▀▀     │\n"
  115.                         "   └───────────────────────┘\n"
  116.                         "D-Flat implements the SAA/CUA\n"
  117.                         "interface in a public domain\n"
  118.                         "C language library originally\n"
  119.                         "published in Dr. Dobb's Journal\n"
  120.                         "    ------------------------ \n"
  121.                         "MemoPad is a multiple document\n"
  122.                         "editor that demonstrates D-Flat");
  123.                     return TRUE;
  124.                 default:
  125.                     break;
  126.             }
  127.             break;
  128.         default:
  129.             break;
  130.     }
  131.     return DefaultWndProc(wnd, msg, p1, p2);
  132. }
  133. /* --- The New command. Open an empty editor window --- */
  134. static void NewFile(WINDOW wnd)
  135. {
  136.     OpenPadWindow(wnd, Untitled);
  137. }
  138. /* --- The Open... command. Select a file  --- */
  139. static void SelectFile(WINDOW wnd)
  140. {
  141.     char FileName[64];
  142.     if (OpenFileDialogBox("*.PAD", FileName))    {
  143.         /* --- see if the document is already in a window --- */
  144.         WINDOW wnd1 = GetFirstChild(wnd);
  145.         while (wnd1 != NULLWND)    {
  146.             if (stricmp(FileName, wnd1->extension) == 0)    {
  147.                 SendMessage(wnd1, SETFOCUS, TRUE, 0);
  148.                 SendMessage(wnd1, RESTORE, 0, 0);
  149.                 return;
  150.             }
  151.             wnd1 = GetNextChild(wnd, wnd1);
  152.         }
  153.         OpenPadWindow(wnd, FileName);
  154.     }
  155. }
  156. /* --- open a document window and load a file --- */
  157. static void OpenPadWindow(WINDOW wnd, char *FileName)
  158. {
  159.     static WINDOW wnd1 = NULLWND;
  160.     struct stat sb;
  161.     char *Fname = FileName;
  162.     char *ermsg;
  163.     if (strcmp(FileName, Untitled))    {
  164.         if (stat(FileName, &sb))    {
  165.             if ((ermsg = malloc(strlen(FileName)+20)) != NULL) {
  166.                 strcpy(ermsg, "No such file as\n");
  167.                 strcat(ermsg, FileName);
  168.                 ErrorMessage(ermsg);
  169.                 free(ermsg);
  170.             }
  171.             return;
  172.         }
  173.         Fname = NameComponent(FileName);
  174.     }
  175.     wndpos += 2;
  176.     if (wndpos == 20)
  177.         wndpos = 2;
  178.     wnd1 = CreateWindow(EDITBOX,
  179.                 Fname,
  180.                 (wndpos-1)*2, wndpos, 10, 40,
  181.                 NULL, wnd, EditorProc,
  182.                 SHADOW     |
  183.                 MINMAXBOX  |
  184.                 CONTROLBOX |
  185.                 VSCROLLBAR |
  186.                 HSCROLLBAR |
  187.                 MOVEABLE   |
  188.                 HASBORDER  |
  189.                 SIZEABLE   |
  190.                 MULTILINE
  191.     );
  192.     if (strcmp(FileName, Untitled))    {
  193.         if ((wnd1->extension =
  194.                 malloc(strlen(FileName)+1)) != NULL)    {
  195.             strcpy(wnd1->extension, FileName);
  196.             LoadFile(wnd1, (int) sb.st_size);
  197.         }
  198.     }
  199.     SendMessage(wnd1, SETFOCUS, TRUE, 0);
  200. }
  201. /* --- Load the notepad file into the editor text buffer --- */
  202. static void LoadFile(WINDOW wnd, int tLen)
  203. {
  204.     char *Buf;
  205.     FILE *fp;
  206.  
  207.     if ((Buf = malloc(tLen+1)) != NULL)    {
  208.         if ((fp = fopen(wnd->extension, "rt")) != NULL)    {
  209.             memset (Buf, 0, tLen+1);
  210.             fread(Buf, tLen, 1, fp);
  211.             SendMessage(wnd, SETTEXT, (PARAM) Buf, 0);
  212.             fclose(fp);
  213.         }
  214.         free(Buf);
  215.     }
  216. }
  217. /* --- print the current notepad --- */
  218. static void PrintPad(WINDOW wnd)
  219. {
  220.     unsigned char *text;
  221.  
  222.     /* ---------- print the file name ---------- */
  223.     fputs("\r\n", stdprn);
  224.     fputs(GetTitle(wnd), stdprn);
  225.     fputs(":\r\n\n", stdprn);
  226.  
  227.     /* ---- get the address of the editor text ----- */
  228.     text = GetText(wnd);
  229.  
  230.     /* ------- print the notepad text --------- */
  231.     while (*text)    {
  232.         if (*text == '\n')
  233.             fputc('\r', stdprn);
  234.         fputc(*text++, stdprn);
  235.     }
  236.  
  237.     /* ------- follow with a form feed? --------- */
  238.     if (YesNoBox("Form Feed?"))
  239.         fputc('\f', stdprn);
  240. }
  241. /* ---------- save a file to disk ------------ */
  242. static void SaveFile(WINDOW wnd, int Saveas)
  243. {
  244.     FILE *fp;
  245.     if (wnd->extension == NULL || Saveas)    {
  246.         char FileName[64];
  247.         if (SaveAsDialogBox(FileName))    {
  248.             if (wnd->extension != NULL)
  249.                 free(wnd->extension);
  250.             if ((wnd->extension =
  251.                     malloc(strlen(FileName)+1)) != NULL)    {
  252.                 strcpy(wnd->extension, FileName);
  253.                 AddTitle(wnd, NameComponent(FileName));
  254.                 SendMessage(wnd, BORDER, 0, 0);
  255.             }
  256.         }
  257.         else
  258.             return;
  259.     }
  260.     if (wnd->extension != NULL)    {
  261.         WINDOW mwnd = MomentaryMessage("Saving the file");
  262.         if ((fp = fopen(wnd->extension, "wt")) != NULL)    {
  263.             fwrite(GetText(wnd), strlen(GetText(wnd)), 1, fp);
  264.             fclose(fp);
  265.             wnd->TextChanged = FALSE;
  266.         }
  267.         SendMessage(mwnd, CLOSE_WINDOW, 0, 0);
  268.     }
  269. }
  270. /* -------- delete a file ------------ */
  271. static void DeleteFile(WINDOW wnd)
  272. {
  273.     if (wnd->extension != NULL)    {
  274.         if (strcmp(wnd->extension, Untitled))    {
  275.             char *fn = NameComponent(wnd->extension);
  276.             if (fn != NULL)    {
  277.                 char msg[30];
  278.                 sprintf(msg, "Delete %s?", fn);
  279.                 if (YesNoBox(msg))    {
  280.                     unlink(wnd->extension);
  281.                     SendMessage(wnd, CLOSE_WINDOW, 0, 0);
  282.                 }
  283.             }
  284.         }
  285.     }
  286. }
  287. /* ------ display the row and column in the statusbar ------ */
  288. static void ShowPosition(WINDOW wnd)
  289. {
  290.     char status[30];
  291.     sprintf(status, "Line:%4d  Column: %2d",
  292.         wnd->CurrLine, wnd->CurrCol);
  293.     SendMessage(GetParent(wnd), ADDSTATUS, (PARAM) status, 0);
  294. }
  295. /* ----- window processing module for the editboxes ----- */
  296. static int EditorProc(WINDOW wnd,MESSAGE msg,PARAM p1,PARAM p2)
  297. {
  298.     int rtn;
  299.     switch (msg)    {
  300.         case SETFOCUS:
  301.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  302.             if ((int)p1 == FALSE)
  303.                 SendMessage(GetParent(wnd), ADDSTATUS, 0, 0);
  304.             else 
  305.                 ShowPosition(wnd);
  306.             return rtn;
  307.         case KEYBOARD_CURSOR:
  308.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  309.             ShowPosition(wnd);
  310.             return rtn;
  311.         case COMMAND:
  312.             if ((int) p1 == ID_HELP)    {
  313.                 DisplayHelp(wnd, "MEMOPADDOC");
  314.                 return TRUE;
  315.             }
  316.             break;
  317.         case CLOSE_WINDOW:
  318.             if (wnd->TextChanged)    {
  319.                 char *cp = malloc(25+strlen(GetTitle(wnd)));
  320.                 SendMessage(wnd, SETFOCUS, TRUE, 0);
  321.                 if (cp != NULL)    {
  322.                     strcpy(cp, GetTitle(wnd));
  323.                     strcat(cp, "\nText changed. Save it?");
  324.                     if (YesNoBox(cp))
  325.                         SendMessage(GetParent(wnd),
  326.                             COMMAND, ID_SAVE, 0);
  327.                     free(cp);
  328.                 }
  329.             }
  330.             wndpos = 0;
  331.             if (wnd->extension != NULL)    {
  332.                 free(wnd->extension);
  333.                 wnd->extension = NULL;
  334.             }
  335.             break;
  336.         default:
  337.             break;
  338.     }
  339.     return DefaultWndProc(wnd, msg, p1, p2);
  340. }
  341. /* -- point to the name component of a file specification -- */
  342. static char *NameComponent(char *FileName)
  343. {
  344.     char *Fname;
  345.     if ((Fname = strrchr(FileName, '\\')) == NULL)
  346.         if ((Fname = strrchr(FileName, ':')) == NULL)
  347.             Fname = FileName-1;
  348.     return Fname + 1;
  349. }
  350.